(function(){

var BusCon = require("../../core/bus/_con");
var GoodsCon = require("../../core/goods/_con");
var db = require("../../core/db/index");
var config = require("../../core/_include/config");
var ResponObj = require("../_include/ResponObj");

//正式的实现应该是获取Session中登陆者的信息
function getBusId(requestQuery,onSuccess,onError){
	var method = requestQuery.method;
	var bus_id_or_name = requestQuery.bus;
	console.log(bus_id_or_name);
	if (bus_id_or_name) {
		if (method==="byname") {
			var bus = db.find_one(config.dbname.bus,{
				info:{
					name:bus_id_or_name
				}
			});
			if (!bus) {
				return onError("找不到指定的商家");
			}
			var bus_id = bus._id;
		}
		bus_id = bus_id_or_name;
		var result = db.find_list(config.dbname.goods,{
			bus_id:bus_id
		});
		// return bus_id;
		onSuccess(bus_id);
	}else{
		onError("缺少相关的商家信息")
	}
};
module.exports = {
	prefix:"/goods",
	get:{
		"/one/:id":function *(next){
			yield next;
		},
		"/list/:name":function *(next){
			yield next;
		},
		//获取商家的所有商品
		//.query.method:find bus "byname"||"byid"
		//.query.bus:bus name or id
		"/:id_or_name":function *(next){
			var self = this;
			//query每次的获取都要重新计算一次，进行缓存
			var requestQuery = self.request.query;
			requestQuery.bus = self.params.id_or_name;
			getBusId(requestQuery,function onSuccess(bus_id){
				var goods_list = db.find_list(config.dbname.goods,{
					bus_id:bus_id
				});
				self.body = ResponObj("json",goods_list);
			},function onError(errorMes){
				self.body = ResponObj("error",errorMes)	
			});
			yield next;
		}
	},
	post:{
		//直接批量加入单种商品
		//.body:goods_info
		//.query.num:goods num||1
		//.query.method:find bus "byname"||"byid"
		//.query.bus:bus name or id
		"/":function *(next){
			var self = this;
			getBusId(self.request.query,function onSuccess(bus_id){
				var goods_list = db.find_list(config.dbname.goods,{
					bus_id:bus_id
				});
				this.body = ResponObj("json",goods_list);
				var bus = BusCon.get(bus_id);
				bus.importGoods(self.request.body,self.request.query.num||1);
				self.body = ResponObj("json","添加商品信息成功");
			},function onError(errorMes){
				self.body = ResponObj("error",errorMes)	
			});
			yield next;
		},
		//添加复合的商品
		//.body.goods:mix_goods:[{goods_info,num}...]
		//.query.method:find bus "byname"||"byid"
		//.query.bus:bus name or id
		"/mix":function *(next){
			var self = this;
			var mix_goods = self.request.body.mix_goods;
			if (mix_goods) {
				try{
					mix_goods = JSON.parse(mix_goods);
				}catch(e){
					console.log(e);
				}
				if (mix_goods instanceof Array) {
					getBusId(self.request.query,function onSuccess(bus_id){
						var bus = BusCon.get(bus_id);
						try{
							mix_goods.forEach(function(goods){
								bus.importGoods(goods.goods_info,goods.num||1);
							});
							self.body = ResponObj("json","添加商品信息成功");
						}catch(e){
							self.body = ResponObj("error",errorMes)	
						}
					},function onError(errorMes){
						self.body = ResponObj("error",errorMes)	
					});
				}else{
					self.body = ResponObj("error","参数错误")
				}
			}else{
				self.body = ResponObj("error","参数错误")
			}
			yield next;
		}
	}
}
}());